package com.yanqu.road.server.logic.activity.condition.patronsfund;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.patronsfund.PatronsFundModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.server.protocol.GameErrorCode;

import java.math.BigInteger;

public class PatronsFundIntelligenceCondition extends BaseActivityCondition {

    public PatronsFundIntelligenceCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
        super(activityInfo, activityConditionInfo, baseActivityData);
    }

    private void changeValue(BigInteger val) {
        setValue(val);
        //玩家日志
        player.getModule(PatronsFundModule.class).savePatronsFundUserData(this.activityInfo.getChildType(), this.activityInfo.getActivityId(), val.longValue(), 0);
        //达成日志
        if (this.isCompleted(player)) {
            player.getModule(PatronsFundModule.class).savePatronsFundConditionCom(this.activityInfo.getChildType(), this.activityInfo.getActivityId(), this.getConditionType(), this.getConditionId());
        }
    }

    @Override
    protected void player_BoardLevelHandle(Object object) {
        //判断是否是对应门客
        UserPatrons userPatrons = (UserPatrons) object;
        if (userPatrons.getPatronsId() == this.activityInfo.getChildType()) {
            //获取门客资质。排除【珍兽】【鱼类】【蛐蛐】【信物】带来的资质
            long qualification = player.getModule(PatronsModule.class).getQualificationExcludeEq(userPatrons.getPatronsId());
            BigInteger val = BigInteger.valueOf(qualification);
            if(val.compareTo(getValue()) > 0) {
                changeValue(val);
            }
        }
    }

    public void initDefaultValue() {
        //获取门客资质。排除【珍兽】【鱼类】【蛐蛐】【信物】带来的资质
        long qualification = player.getModule(PatronsModule.class).getQualificationExcludeEq(this.activityInfo.getChildType());
        BigInteger val = BigInteger.valueOf(qualification);
        changeValue(val);
    }

    public boolean isCompleted(GamePlayer player) {
        //门客id
        int patronsId = this.activityInfo.getChildType();
        //第一位是是否要購買過
        if(activityConditionInfo.getParamList().get(0).compareTo(BigInteger.ONE) == 0){
            //是否購買了
            if(player.getModule(PatronsFundModule.class).checkIsBuyPatronsFund(patronsId)){
                return getValue().compareTo(activityConditionInfo.getParamList().get(3)) >= 0;
            }
        }else if(activityConditionInfo.getParamList().get(0).compareTo(BigInteger.ZERO) == 0){
            return getValue().compareTo(activityConditionInfo.getParamList().get(3)) >= 0;
        }
        return false;
    }

    @Override
    public void addTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.PatronsFundIntelligence.getValue(), handle);
    }

    @Override
    public void removeTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.PatronsFundIntelligence.getValue(), handle);
    }
}
